<template>
	<view class="page">
		<u-navbar title="版本服务" border @leftClick="$utils.back()" placeholder>
		</u-navbar>
		<view class="body">
			<scroll-view :scroll-y="true" style="height:100% ;">
				<view class="contentBox">
					<u-steps current="1" activeColor='green'>
						<u-steps-item title="创建订单">
						</u-steps-item>
						<u-steps-item title="确认订单"></u-steps-item>
						<u-steps-item title="支付订单"></u-steps-item>
						<u-steps-item title="交易完成"></u-steps-item>
					</u-steps>
				</view>
				<view class="contentBox padding">
					<view class="box">
						<view v-if="loading" class="titleBox"></view>
						<view v-else class="titleBox">
							{{data.title}}
						</view>
						<view v-if='loading'>
							<view class="Skeleton" style="height: 30rpx;w" />
								
							<view style="display: flex;justify-content: space-between;">
								<view class="Skeleton" style="height: 30rpx;width:100rpx" />
								<view class="Skeleton" style="height: 30rpx;width:100rpx" />
							</view>
						</view>
						<view v-else>
							<view>
								描述：{{data.description}}
							</view>
							<view style="display: flex;justify-content: space-between;">
								<text style="color: #bbb;">
									￥{{charge}}
								</text>
								<text style="color: #bbb;">
									x1
								</text>
							</view>
						</view>
					</view>
				</view>
				<view class="contentBox padding">
					<view class="mb20">
						<u-icon name='shopping-cart' label="有效期" size="30rpx" labelSize="30rpx" />
					</view>
					<view v-if="loading" class="expiredsBox">
						<view v-for="item in 4" :key="item" class="Skeleton expiredsItem" style="height: 30rpx;" />
					</view>
					<view class="expiredsBox" v-else>
						<view @click="expiredsIndex=index" v-for="(item,index) in expireds"
							:class="index==expiredsIndex?'chooseExpireds':''" :key="item.id" class="expiredsItem">
							{{item.name}}
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
		<view class="footer">
			<view style="width: 600rpx; ">
				<u-button type="primary" color='red' :text="'提交订单￥'+charge" throttleTime="5000" shape="circle"
					@click="sumbit"></u-button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				data: {},
				loading: true,
				expireds: [],
				expiredsIndex: 0,
			}
		},
		onLoad(op) {
			this.$api_company.Platform.Detail(op.id).then(res => {
				this.data = res.data.role
				this.expireds = this.data.expireds
				this.loading = false
			})
		},
		computed: {
			charge() {
				if (!this.expireds.length) return 0
				return this.expireds[this.expiredsIndex].charge
			}
		},
		methods: {
			sumbit() {
				if (this.charge == 0) return
				let obj = {
					expired_id: this.expireds[this.expiredsIndex].id,
					role_id: this.data.id
				}
				this.$api_company.Platform.Order.Create(obj).then(res => {
					this.pay({
						openid: uni.getStorageSync("openid"),
						order_id: res.data.order_id,
						id: res.data.id
					})
				})
			},
			pay(params) {
				this.$api_company.Order.postOrder(params).then(res => {
					uni.requestPayment({
						provider: "wxpay",
						appid: res.data.appId, // 微信开放平台 - 应用 - AppId，注意和微信小程序、公众号 AppId 可能不一致
						nonceStr: res.data.nonceStr, // 随机字符串
						package: res.data.package, // 固定值
						prepayId: res.data.package.substring(10), // 统一下单订单号 
						timeStamp: res.data.timeStamp, // 时间戳（单位：秒）
						signType: res.data.signType, // 签名，这里用的 MD5/RSA 签名
						paySign: res.data.paySign,
						success(res) {
							uni.reLaunch({
								url:'/shop/index'
							})
						},
						fail(e) {
							
						}
					})
				})
			}
		}
	}
</script>

<style scoped lang="less">
	.page {
		height: 100vh;
		padding-bottom: calc(constant(safe-area-inset-bottom) + 10rpx);
		/* 兼容 iOS 设备 */
		padding-bottom: calc(env(safe-area-inset-bottom) + 10rpx);
		box-sizing: border-box;
		display: flex;
		flex-direction: column;
		background-color: #f5f5f5;
	}

	.body {
		flex: 1;
		overflow: hidden;
	}

	.contentBox {
		margin: 0 0 20rpx;
		background-color: #fff;
		padding: 20rpx 0;
	}

	.padding {
		padding: 20rpx 40rpx;
	}

	.box {
		// margin-top:  var(--status-bar-height) ;//给组件加个上边距
		justify-content: space-between;
		font-size: 26rpx;
		color: #010101;
		height: 180rpx;
		display: flex;

		>view:nth-child(2) {
			margin-left: 20rpx;
			height: 180rpx;
			display: flex;
			flex: 1;
			flex-direction: column;
			justify-content: space-between;
		}
	}

	.titleBox {
		font-size: 24rpx;
		line-height: 180rpx;
		text-align: center;
		color: rgba(255, 255, 255, 0.8);
		width: 180rpx;
		height: 180rpx;
		border-bottom: 1rpx solid rgba(255, 255, 255, 0.4);
		border-left: 1rpx solid rgba(255, 255, 255, 0.4);
		background: linear-gradient(to top right, rgba(90, 149, 207, 0.5), rgba(58, 76, 99, 0.8));
		-webkit-backdrop-filter: blur(6rpx);
		backdrop-filter: blur(6rpx);
		border-radius: 20rpx;
	}

	.expiredsBox {
		display: flex;
		justify-content: space-between;

		>view {
			width: 22.5%;
		}
	}

	.expiredsItem {
		padding: 10rpx 0;
		background: #eee;
		border-radius: 8rpx;
		margin-top: 20rpx;
		text-align: center;
	}

	.chooseExpireds {
		border: 1px solid #3975c6;
		color: #3975c6;
	}

	.footer {
		align-items: center;
		width: 100vw;
		color: #010101;
		display: flex;
		justify-content: center;
		box-sizing: border-box;
	}
</style>